package com.haiqi.modules.main.web;

import com.haiqi.common.persistence.dao.CommunicationInfoMapper;
import com.haiqi.modules.main.bean.MenuResponse;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.main.service.GetInfoService;
import com.haiqi.modules.main.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hythzx on 2016/5/10.
 */
@Controller
public class IndexController {

    @Autowired
    private CommunicationInfoMapper communicationInfoMapper;

    @Autowired
    private GetInfoService infoService;

    @Autowired
    private UserService userService;



    @RequestMapping("/getJson")
    @ResponseBody
    public String getData(String name){
        Map map = new HashMap();
        map.put("name",name);
        return "login";
    }


    @RequestMapping("/")
    public String main(HttpSession session){
        Object user = session.getAttribute("user");
        if(user == null){
            return "redirect:/login";
        }else{
            return "index";
        }
    }

    @RequestMapping("/index")
    public String index(HttpSession session){
        Object user = session.getAttribute("user");
        if(user == null){
            return "redirect:/login";
        }else{
            return "index";
        }
    }


    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(Model model,HttpServletRequest request){
        Object basePath = request.getSession().getAttribute("basePath");
        return "modules/user/login";
    }

    @RequestMapping("do-login")
    public String doLogin(Model model, @RequestParam("username") String userName, String password, HttpSession session){
        ResponseBean responseBean = null;
        try {
            responseBean = userService.login(userName, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(responseBean != null){
            if(responseBean.isSuccess()){
                session.setAttribute("user",responseBean.getUser());
                try {
                    MenuResponse menuResponse = userService.getMenus(responseBean.getUser().getId(), responseBean.getToken());
                    if(menuResponse.isSuccess()){
                        session.setAttribute("menu",menuResponse.getMenus());
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return "redirect:/index";
            }else{
                model.addAttribute("errorMsg",responseBean.getResponse());

            }
        }else{
            model.addAttribute("errorMsg","系统错误");
        }
        return "modules/user/login";
    }

    @RequestMapping("/error_404")
    public String pageNotFound(){
        return "error/404";
    }

    @RequestMapping("/error_403")
    public String noAuth(){
        return "error/403";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session, Model model){
        session.removeAttribute("user");
        session.removeAttribute("menu");
        session.removeAttribute("token");
        model.addAttribute("errorMsg","");
        return "modules/user/login";
    }


}
